package com.shou.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public final class ValidatorUtils  {
	/**
	 * 邮件的正则表达式
	 */
	private static String EMAIL_REGEX = ".+[@]{1}.+[.]{1}[\\w]+";
	/**中文的正则表达式 第一部分：汉字 第二部分：半角 ([\u4E00-\u9FA5]|[\uFE30-\uFFA0])+*/
	private static String CHINESE_REGEX="^[\u4E00-\u9FA5]+$";
	/**字符的的正则表达式*/
	private static String NUMBERCHAR_REGEX = "^[a-zA-Z0-9]+$";
	/**手机号码正则表达式*/
	private static String PHONE_REGEX = "^1[3|4|5|8|7][0-9]\\d{8}+$";
	
	/**
	 * 检查必填
	 * @param input
	 * @return
	 */
	public static boolean requiredObject(Object obj){
		boolean success = true;
		if(obj == null){
			success = false;
		}else{
			if(obj instanceof String  && String.valueOf(obj).trim().length() == 0){
				success = false;
			}
		}
		return success;
	}
	
	/**
	 * 检查字符串
	 * @param input
	 * @return boolean
	 */
	public static boolean regexString(String input,String regex){
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(input);
		return m.matches();
	}
	
	/**
	 * 检查手机号码
	 * @param input
	 * @return boolean
	 */
	public static boolean isPhoneNumber(String input){
		Pattern p = Pattern.compile(PHONE_REGEX);
		Matcher m = p.matcher(input);
		return m.matches();
	}
	
	/**
	 * isNumberChar
	 * @param input
	 * @return boolean
	 */
	public static boolean isNumberChar(String input){
		Pattern p = Pattern.compile(NUMBERCHAR_REGEX);
		Matcher m = p.matcher(input);
		return m.matches();
	}
	/**
	 * isChinese
	 * @param input
	 * @return boolean
	 */
	public static boolean isChinese(String input){
		Pattern p = Pattern.compile(CHINESE_REGEX);
		Matcher m = p.matcher(input);
		return m.matches();
	}
	/**
	 * isEmailAddress
	 * 
	 * @param input input string
	 * @return boolean
	 * 
	 */
	public static boolean isEmailAddress(String input) {
		Pattern p = Pattern.compile(EMAIL_REGEX);
		Matcher m = p.matcher(input);
		return m.matches();
	}
	/**
	 * isAllLetterOrDigit
	 * 
	 * @param input input string
	 * @return boolean
	 * 
	 */
	public static boolean isAllLetterOrDigit(String input) {

		if (input == null || input.length() < 1)
			return false;
		char[] c = input.toCharArray();
		for (int i = 0, m = c.length; i < m; i++) {
			if (!Character.isLetterOrDigit(c[i]))
				return false;
		}
		return true;

	}
	/**
	 * isDigit
	 * 
	 * @param input input String
	 * @return boolean
	 * 
	 */
	public static boolean isDigit(String input) {
		try {
			Integer.parseInt(input);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}
	/**
	 * isFilePathValid
	 * 
	 * @param input input string
	 * @return boolean
	 * 
	 */
	public static boolean isFilePathValid(String input) {

		if (input == null || input.length() < 1)
			return false;
		char[] c = input.toCharArray();
		for (int i = 0, m = c.length; i < m; i++) {
			if (!Character.isLetterOrDigit(c[i]) && c[i] != '/' && c[i] != '-' && c[i] != '_')
				return false;
		}
		return true;
	}
	public static boolean isEmpty(String str) {
		if (str != null && str.trim().length() > 0)
			return false;
		return true;
	}
	
	
	/*
	 * 检查验证码用途
	 */
	public static boolean checkUseType(Integer useType){
		if(useType ==null){
			return false;
		}else if(useType==1 || useType ==2 || useType ==3 || useType ==4 || useType ==5){
			return true;
		}else{
			return false;
		}
	}
	
	/*
	 * 检查验证码
	 */
	public static boolean checkVcode(String vcode){
		if(StringUtils.isBlank(vcode)){
			return false;
		}else if(ValidatorUtils.regexString(vcode, "^\\d{6}+$")){
			return true;
		}else{
			return false;
		}
	}
	
	/*
	 * 检查验证码
	 */
	public static boolean checkPhone(String phone){
		if(StringUtils.isBlank(phone)){
			return false;
		}else if(ValidatorUtils.isPhoneNumber(phone)){
			return true;
		}else{
			return false;
		}
	}
}
